--[[
	Galactica
	File: saveload.lua
]]

function Galactica.MakeQuotesSafe(Text)
	return string.Replace(Text, "\"", "**")
end

function Galactica.ReventQuotes(Text)
	return string.Replace(Text, "**", "\"")
end


--Ok were going to recurse through the table and serialize everything
function Galactica.MakeStringTableSafe(Table)
	local theType = type(Table)
	if theType == "string" then
		Table = Galactica.MakeQuotesSafe(Table)
	elseif(theType == "table") then
		for k, v in pairs(Table) do
			theType = type(v)
			if theType == "string" then
				v = Galactica.MakeQuotesSafe(v)
			elseif theType == "table" then
				Galactica.MakeStringTableSafe(v)
			end
		end	
	end
	
	return Table
end

function Galactica.MakeStringTableRevert(Table)	
	for k, v in pairs(Table) do
		Table[k] = Galactica.ReventQuotes(v)
	end
	return Table
end

function Galactica.FileSaveName(ply)
	return "Galactica/"..string.Replace(ply:SteamID(), ":", ".")..".txt"
end

function Galactica.SaveData(ply)
	file.Write(Galactica.FileSaveName(ply), util.TableToKeyValues(Galactica.MakeStringTableSafe(Galactica.Players[ply:SteamID()])))
end

function Galactica.LoadData(ply)
	if(!file.Exists(Galactica.FileSaveName(ply))) then return false end
	local File = file.Read(Galactica.FileSaveName(ply))
	Galactica.Players[ply:SteamID()] = Galactica.MakeStringTableRevert(util.KeyValuesToTable(File))
	return true
end
